package com.gxd.item_75;

public class Solution {
    public void sortColors(int[] nums) {
        if (nums == null || nums.length == 0) {
            throw new IllegalArgumentException("invalid param");
        }

        int start = 0, i = 0;
        int end = nums.length - 1;

        while (i <= end) {
            if (nums[i] == 0) {
                swap(nums, i, start);
                i++;
                start++;
            } else if (nums[i] == 1) {
                i++;
            } else {
                swap(nums, i, end);
                end--;
            }
        }
    }

    public void swap(int[] nums, int index1, int index2) {
        int temp = nums[index1];
        nums[index1] = nums[index2];
        nums[index2] = temp;
    }
}
